//
//  LFDBHelper.m
//  AlarmClock
//
//  Created by LF on 2017/4/26.
//  Copyright © 2017年 LF. All rights reserved.
//

#import "LFDBHelper.h"

#import "FMDatabase.h"

/*
 下载记录表
 status : 0、等待下载 1、正在下载 2、已下载 3、下载失败，等待下次重试
 
 |- id 主键自增 -|- track_id -| - album_id - | - status - | - create_ime - | - update_time - |
 */

static NSString *kDBTableCreateSQL = @"CREATE TABLE IF NOT EXISTS my_alarm_clock_cache(id integer primary key autoincrement,alarm_model blob NOT NULL, identifier text NOT NULL);";

@interface LFDBHelper ()

@property (nonatomic, strong) FMDatabase *dataBase;

@end

@implementation LFDBHelper

+ (instancetype)helper {
    LFDBHelper *helper = [[LFDBHelper alloc] init];
    [helper createDataBaseTable];
    return helper;
}

#pragma mark - 更新记录
- (void)updateAlarmClock:(LFAlarmClockModel *)model {
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
    //NSString *sql = [NSString stringWithFormat:@"update my_alarm_clock_cache set alarm_model = %@ where identifier = %@",model,model.identifier];
    [self.dataBase open];
    [self.dataBase executeUpdate:@"UPDATE my_alarm_clock_cache SET alarm_model = (?) WHERE identifier = (?)",data,model.identifier];
    [self.dataBase close];
}

#pragma mark - 查询记录
/*
 *  查询某一条记录是否存在
 */
- (void)itemExistsForAlarmClock:(LFAlarmClockModel *)model withResultBlock:(void (^)(BOOL))result {
    //NSString *sql = [NSString stringWithFormat:@"select * from my_alarm_clock_cache where identifier = %@",model.identifier];
    [self.dataBase open];
    FMResultSet *rs = [self.dataBase executeQuery:@"SELECT * FROM my_alarm_clock_cache WHERE identifier = (?)",model.identifier];
    if (rs.next) {
        result(YES);
    } else {
        result(NO);
    }
    // 这里是在DB关闭之前把结果回调出去
    [self.dataBase close];
//    return result.next;
}

/*
 * 查询所有记录
 */
- (NSArray *)queryAlarmsFromDB {
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM my_alarm_clock_cache order by id"];
    [self.dataBase open];
    FMResultSet *result = [self.dataBase executeQuery:sql];
    NSMutableArray *arr = [NSMutableArray new];
    while (result.next) {
        NSData *data = [result dataForColumn:@"alarm_model"];
        LFAlarmClockModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        [arr addObject:model];
    }
    [self.dataBase close];
    return arr;
}

#pragma mark - 插入记录
/*
 * 插入一条新的下载记录
 */
- (void)insertAlarmClock:(LFAlarmClockModel *)model {
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
//    NSString *sql = [NSString stringWithFormat:@"insert into my_alarm_clock_cache(alarm_model,identifier) values (%@,%zd);",model,model.identifier];

    [self.dataBase open];
    [self.dataBase executeUpdate:@"INSERT INTO my_alarm_clock_cache (alarm_model, identifier) VALUES (?, ?);",data,model.identifier];
    [self.dataBase close];
}

/*
 *  删除数据库中对应的记录
 */
- (void)deleteAlarmClock:(LFAlarmClockModel *)model {
    [self.dataBase open];
    [self.dataBase executeUpdateWithFormat:@"DELETE FROM my_alarm_clock_cache WHERE identifier = %@;", model.identifier];
    [self.dataBase close];
}

#pragma mark - 数据库创建
/*
 *   创建数据库表
 */
- (void)createDataBaseTable {
    //打开数据库
    [self.dataBase open];
    //执行创建数据库操作
    [self.dataBase executeUpdate:kDBTableCreateSQL];
    //关闭数据库
    [self.dataBase close];
}

#pragma mark - getter
- (FMDatabase *)dataBase {
    if(!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self dataBasePath]];
    }
    return _dataBase;
}

- (NSString *)dataBasePath {
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    return [path stringByAppendingPathComponent:@"fmdb_my_alarm_clock_cache_db.sqlite"];
}


@end
